<?php
namespace BoxMall\Controller\Admin;
//use Think\Controller;
class GoodsController extends CommonController {
	public function _initialize() {
		parent::_initialize();

	}

	/******商品列表****/
	public function index() {
		$data = BoxModel("mall_goods")->get(array('store_id' => $this->admin_store_id), "10");
		$this->data = $data['list'];
		$this->show_page($data['pager']);
		$this->display();
	}

	/******添加商品****/
	public function add() {
		$this->pidProperty = BoxModel("mall_property")->get(array('store_id' => $this->admin_store_id, 'pid' => 0));
		//dump($this->pidProperty);
		$this->propertys = BoxModel("mall_property")->get(array('store_id' => $this->admin_store_id));
		$id = I('id');
		$model = BoxModel("mall_goods");
		$model2 = BoxModel("mall_goods_spec");
		if ($id) {
			$this->data = $model->load($id);
		}
		$this->property = $this->data['property'] ? $this->data['property'] : "{}";
		$this->goods_spec = $this->data['goods_spec'] ? $this->data['goods_spec'] : "{}";
		//dump($this->property);
		if (IS_POST) {
			$imgs = "";
			$imglist = I('imgList');
			$property = htmlspecialchars_decode(I('property'));
			$goods_spec = htmlspecialchars_decode(I('goods_spec'));
			$row = $model->create();
			$row['store_id'] = $this->admin_store_id;
			$row['content'] = strip_tags(htmlspecialchars_decode(I('content')), dict('allow_tags'));
			if (is_array($imglist)) {
				$row['imgs'] = json_encode($imglist);
			}
			$row['property'] = $property;
			//dump($property);exit();
			if ($id) {
				$save_goods = $model->where(array('id' => $id))->save($row);
			} else {
				$row['addtime'] = time();
				$id = $save_goods = $model->add($row);
			}
			//保存成功
			if ($save_goods !== false) {
				$goods_spec_array = json_decode($goods_spec, true);
				//dump($goods_spec_array);
				if (!empty($goods_spec_array)) {
					foreach ($goods_spec_array as $key => $value) {
						$keys = explode("_", $key);
						$spec_row[] = array(
							'spec_1' => $keys[0],
							'spec_2' => $keys[1],
							'spec_3' => $keys[2],
							'goods_id' => $id,
							'spec_price' => is_numeric($value['spec_price']) ? $value['spec_price'] : 0,
							'spec_stock' => intval($value['spec_stock']),
							'spec_code' => $value['spec_code'],
						);
					}
				} else {
					$spec_row[] = array(
						'spec_1' => "",
						'spec_2' => "",
						'spec_3' => "",
						'goods_id' => $id,
						'spec_price' => is_numeric($row['price']) ? $row['price'] : 0,
						'spec_stock' => intval($row['total_stock']),
						'spec_code' => $row['code'],
					);
				}
				//dump($spec_row);exit();
				$model2->where(array('goods_id' => $id))->delete();
				foreach ($spec_row as $key => $value) {
					$r = $model2->add($value);
					if ($r === false) {
						$error = "规格信息保存失败";
					}
				}

			}

			if ($save_goods === false) {
				$this->jump("保存失败", U('add', array('goods_id' => $id)));
			} else if ($error) {
				$this->jump($error, U('add', array('goods_id' => $id)));
			} else {
				$this->jump("保存成功");
			}

		}
		$this->display();
	}

	/***保存property**/
	public function setProperty() {
		$row = array(
			'store_id' => $this->admin_store_id,
			'lable' => I('lable'),
			'pid' => I('pid', "0", "intval"),
		);
		$r = BoxModel("mall_property")->where($row)->find();
		if (!$r) {
			$id = BoxModel("mall_property")->add($row);
			echo $id;
		} else {
			echo $r['id'];
		}
	}

}